package com.BookManager.service.Impl;

import com.BookManager.entity.Book;
import com.BookManager.entity.Lend;
import com.BookManager.mapper.BookMapper;
import com.BookManager.service.BookService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    BookMapper mapper;

    @Override
    public List<Lend> getLendList() {
        return mapper.getLendList();
    }

    @Override
    public Map<Book,Boolean> getBookMap() {
        Set<Integer> set = new HashSet<>();
        this.getLendList().forEach(lend -> set.add(lend.getBid()));
        Map<Book,Boolean> map = new LinkedHashMap<>();
        for (Book book : mapper.getBookList()) {
            map.put(book, set.contains(book.getBid()));
        }
        return map;
    }

    @Override
    public List<Book> getActiveBookList() {
        Set<Integer> set = new HashSet<>();
        this.getLendList().forEach(lend -> set.add(lend.getBid()));
        return mapper
                .getBookList()
                .stream()
                .filter(book -> !set.contains(book.getBid()))
                .toList();
    }

    @Override
    public void addLend(int sid,int bid) {
        mapper.addLend(sid, bid);
    }

    @Override
    public void returnBook(int lid) {
        mapper.deleteLend(lid);
    }

    @Override
    public void addBook(String title, String info, double price) {
        mapper.addBook(title,info,price);
    }

    @Override
    public void deleteBook(int bid) {
        mapper.deleteBook(bid);
    }
}
